#!/usr/bin/python2.6
#
# Copyright 2009 Google Inc. All Rights Reserved.

"""Library to handle data request with Yahoo."""

# Python language specific imports.
import Cookie
import datetime
import math
import logging
import os
import time
import urllib

# AppEngine imports.
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util


class Feed(webapp.RequestHandler):
  """The class to handle all yahoo mail related requests."""

  def RenderTemplate(self, filename, dictionary):
    """Render the given template definition file with the given dictionary.

    Args:
      filename: The filename of the template to expand.
      dictionary: The dictionary that contains the values to populate
          in the template.
    """
    self.response.out.write(template.render(filename, dictionary))

  def get(self, *args):
    """The initial function call."""
    url = self.request.get('url', '')
    if url:
      self.response.headers['Content-Type'] = 'application/xml'
      try:
        file = urllib.urlopen(url)
        text = file.read()
        self.response.out.write(text)
      except:
        self.response.out.write('<root><Error>Error occured</Error></root>')
    else:
      self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
      self.RenderTemplate('templates/error.html', {})

  def post(self, *args):
    self.get(*args)


def main():
  application = webapp.WSGIApplication([(r'/makeRequest', Feed),
                                        (r'/.*', Feed)],
                                        debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()
